<?php
class gather_lib {

    /**
     * 获取html内容
     *
     * @param unknown_type $url
     * @param unknown_type $cfile
     */
    public static function gethtml($url , $cfile = false){
        $html = curl_lib::get($url);
        // 编码转换
        preg_match('/<meta.+?charset=[^\w]?([-\w]+)/is' , $html , $m);
        if($m[1] != 'utf-8'){
            $html = mb_convert_encoding($html , 'utf-8'  , $m[1]);
        }

        if($cfile){
            @file_put_contents($cfile ,  $html , LOCK_EX);
        }else{
            return $html;
        }
    }

    public static function debug($ga , $id , $k , $r){
        $ret   = array('code' => 0 , 'msg' => '<span style="color:Red">失败了 , 请检查匹配规则</span>');
        $html  = '';
        $rule  = trim($ga[$r]);
        $cfile = CACHE_PATH . 'ga/lsturl.' . $id;
        // 创建缓存目录
        if(!is_dir(dirname($cfile)) ){
            core::makedir(dirname($cfile));
        }
        // 检查首页缓存文件是否生成
        if(isset($ga['lsturl']) && !is_file($cfile)){
            self::gethtml($ga['lsturl'] , $cfile);
            core::getobj('gather_tmp_tab')->set('k' , $r . '_' . $id)->set('v' , $ga['lsturl'])->replace();
        }
        switch ($r){
            // 目标站首页
            case 'lsturl'   :
                // 更新缓存
                self::gethtml($ga['lsturl'] , $cfile);
                $ret['msg'] = '抓取页面内容成功!';
                break;
            case 'curl'     :
                preg_match_all('/(' . $rule . ').+?\>(.+?)\</is' , file_get_contents($cfile) , $m);
                $tmp = $clist = array();
                foreach ($m[1] as $k => $v){
                    preg_match('/\d+/' , $v , $num);
                    $num = intval($num[0]);
                    if(!isset($tmp[$num]) && $num > 1){
                        core::getobj('gather_tmp_tab')->set('k' , $r . '_' . $id)->set('v' , $v);
                        core::getobj('gather_tmp_tab')->set('c' , $k)->set('n' , filter::text($m[2][$k]))->replace();
                        $tmp[$num] = 1;
                        $num = array();
                    }
                }
                $ret['msg'] = '抓取到 ' . count($tmp) . ' 条';
                $tmp = null; unset($tmp);
                break;
            case 'iurl'     :
            case 'purl'     :
                preg_match_all('/(' . $rule . ')/i' , file_get_contents($cfile) , $m);
                $ret['msg'] = '抓取到 ' . count($m[1]) . ' 条';
                // todo 完整路径 , 相对路径检测
                $url = $m[1][0];
                if($url != ''){
                    $url = $ga['lsturl'] . $url;
                    if($r == 'iurl'){
                        self::gethtml($url , CACHE_PATH . 'ga/iurl.' . $id);
                    }
                    core::getobj('gather_tmp_tab')->set('k' , $r . '_' . $id)->set('v' , $m[1][0])->replace();
                }
                break;
        }
        return $ret;
    }

    /**
     * 新增采集
     *
     * @param unknown_type $ga
     * @return unknown
     */
    public static function add($ga){
        $obj = core::getobj('gather_tab');
        $id = intval($ga['id']);
        $obj->set('id'    , $id);
        $obj->set('name'  , isset($ga['name']) ? $ga['name'] : ('采集_' .date('YmdHi' , REQUEST_TIME)));
        $obj->set('nums'  , 0);
        $obj->set('extra' , 0);
        $obj->set('desc'  , base64_encode(serialize($ga)));
        $obj->set('adate' , isset($ga['adate']) ? $ga['adate'] : REQUEST_TIME);
        $obj->set('lastdate' , REQUEST_TIME);
        $ret = $obj->save();
        return $id > 0 ? $id : intval($ret['msg']);
    }

    /**
     * 获取采集记录信息
     *
     * @param unknown_type $id
     * @return unknown
     */
    public static function get($id){
        $ret = core::getobj('gather_tab')->where("id = " . intval($id))->get();
        if($ret){
            $tmp = unserialize(base64_decode($ret['desc']));
            unset($ret['desc']);
            $tmp = array_merge($ret , $tmp);
            return $tmp;
        }
        return array();
    }
}
?>